<?php

/*
	info: 积分兑换
	date: 2011-12-23
*/

define('PAGESCRIPT', 'change');
define('USER_LOGIN_CHECK', TRUE);
require dirname(__FILE__).'/include/common.inc.php';
$webglobal['file_change'] = array();
//查看详情
if ($webglobal['action'] == 'view')
{
	$webrequest['id'] = isset($webrequest['id']) ? intval($webrequest['id']) : 0;
	if ($webrequest['id']<=0)
	{
		global_ShowMessage("参数错误。");
	}
	$webglobal['datainfo'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}integral_goods WHERE id='{$webrequest['id']}' AND status>=0");

	if (empty($webglobal['datainfo']))
	{
		global_ShowMessage("参数错误。");
	}
	if ($webglobal['datainfo']['image']) {
		$webglobal['datainfo']['thumbnail'] = array(
			'big' => getThumbnails($webglobal['datainfo']['image'], 'big'),
			'small' => getThumbnails($webglobal['datainfo']['image'], 'small'),
			'middle' => getThumbnails($webglobal['datainfo']['image'], 'middle'),
		);
	}
	$webglobal['datainfo']['addtime_show'] = global_FormatTime($webglobal['datainfo']['addtime']);
}
//兑换
elseif ($webglobal['action'] == 'dochange')
{
	$webrequest['count'] = isset($webrequest['count']) ? intval($webrequest['count']) : 0;
	$webrequest['id'] = isset($webrequest['id']) ? intval($webrequest['id']) : 0;
	if ($webrequest['id']<=0 || $webrequest['count']<1)
	{
		global_ShowMessage("参数错误。");
	}

	if ($user['base_uid']<=0)
	{
		global_ShowMessage("请登录后操作。", WEBURLPATH.'/login.php?gourl='.urlencode(WEBURLPATH."/change.php?action=view&id={$webrequest['id']}"));
	}
	if ($user['base_type']!=0)
	{
		global_ShowMessage("只有个人用户才能兑换！", '', 'BACK');
	}

	$webglobal['datainfo'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}integral_goods WHERE id='{$webrequest['id']}' AND status='1'");
	if (empty($webglobal['datainfo']))
	{
		global_ShowMessage("参数错误。");
	}
	$tempneedscore = $webglobal['datainfo']['score']*$webrequest['count'];
	if ($webglobal['datainfo']['score']>0 && $user['score']<$tempneedscore)
	{
		global_ShowMessage("您当前积分为{$user['score']}，积分不够兑换！", '', 'BACK');
	}
	if ($tempneedscore > 0)
	{
		$db->Update('user', "score=score-{$tempneedscore}", "id='{$user['base_uid']}'");
	}
	$db->Insert('integral_order', array(
						'integral_goods_id' => $webglobal['datainfo']['id'],
						'user_id' => $user['base_uid'],
						'unit_score' => $webglobal['datainfo']['score'],
						'quantity' => $webrequest['count'],
						'status' => 0,
						'addtime' => $webserver['timestamp'],
					), TRUE);
	global_ShowMessage("积分兑换订单下单成功，请等待管理员审核。", WEBURLPATH.'/my.php?action=credit');
}
//列表
else
{
	$webrequest['score'] = isset($webrequest['score']) ? $webrequest['score'] : '';
	$webglobal['perpage'] = 9;
	$tempsearchsql = array("status='1'");
	$webglobal['searchurl'] = "";
	if ($webrequest['score'] != '')
	{
		$webglobal['searchurl'] = "&score={$webrequest['score']}";
		$tempscorelimit = global_Intval(explode("-", $webrequest['score']));
		if (!isset($tempscorelimit[1]))
		{
			$tempsearchsql[] = "score>={$tempscorelimit[0]}";
		}
		elseif ($tempscorelimit[1]<=$tempscorelimit[0])
		{
			$tempsearchsql[] = "score={$tempscorelimit[0]}";
		}
		else
		{
			$tempsearchsql[] = "score>={$tempscorelimit[0]}";
			$tempsearchsql[] = "score<={$tempscorelimit[1]}";
		}
	}
	$tempsearchsql = empty($tempsearchsql) ? '' : ' WHERE '.implode(' AND ', $tempsearchsql);
	$webglobal['datanum'] = $db->GetNum("SELECT COUNT(*) FROM {$webconfig['dbpre']}integral_goods {$tempsearchsql}");
	if ($webglobal['datanum']>0)
	{
		$webglobal['page'] = global_Page($webglobal['datanum'], $webglobal['perpage'], $webrequest['page'], WEBURLPATH."/change.php?{$webglobal['searchurl']}&page=@@");
		$tempstartnum = ($webrequest['page']-1)*$webglobal['perpage'];
		$query = $db->Query("SELECT * FROM {$webconfig['dbpre']}integral_goods {$tempsearchsql} ORDER BY id DESC LIMIT $tempstartnum, {$webglobal['perpage']}");
		$webglobal['dataarray'] = array();
		while ($tempdata = $db->FetchArray($query))
		{
			if ($tempdata['image']) {
				$tempdata['thumbnail'] = array(
					'big' => getThumbnails($tempdata['image'], 'big'),
					'small' => getThumbnails($tempdata['image'], 'small'),
					'middle' => getThumbnails($tempdata['image'], 'middle'),
				);
			}

			$tempdata['addtime_show'] = global_FormatTime($tempdata['addtime']);
			$webglobal['dataarray'][$tempdata['id']] = $tempdata;
		}
	}
}

template_Display('change');
